package com.gimi.cloud.jimi.queue.revicer.log;

import com.gimi.cloud.jimi.common.mq.enume.QueueConstant;
import com.gimi.cloud.jimi.dto.log.LogOperationDTO;
import com.gimi.cloud.jimi.queue.service.log.LogOperationService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 操作日志接收对象
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-02-07
 **/
@Component
public class LogOperationRevicer {
    private static final Logger logger = LoggerFactory.getLogger(LogOperationRevicer.class);

    private LogOperationService logOperationService;

    @Autowired
    public LogOperationRevicer(LogOperationService logOperationService) {
        this.logOperationService = logOperationService;
    }

    /**
     * 存储日志
     * @param message
     * @param channel
     * @param logOperation
     * @throws IOException
     */
    @RabbitListener(queues = {QueueConstant.OPERATIONLOG_QUEUE_NAME})
    public  void changeQueueStatus(Message message, Channel channel, LogOperationDTO logOperation) throws IOException {
        try {
            logOperationService.saveOperationLog(logOperation);
        }catch (Exception e){
            logger.info("记录日志失败");
            e.printStackTrace();
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }

    }
}
